import Vue from 'vue';
//引进resource插件并定义城对象
import VueResource from 'vue-resource'
import iView from 'iview';
import loding from './loading.js'
import {constant} from 'aoyun-base'


Vue.use(iView);
//安装定义好 的  resource 插件
Vue.use(VueResource)
Vue.http.options.emulateJSON = true
Vue.http.options.emulateHTTP = true
//定义数据接口
let API_ROOT = ''
if (constant.APIENV === 'prod') {
    API_ROOT = 'https://bmp.aoyunsoft.com/openapi';
} else if (constant.APIENV === 'tst') {
    API_ROOT = 'http://bmptst.aoyunsoft.com/openapi';
} else if (constant.APIENV === 'dev') {
    API_ROOT = 'http://bmptst.aoyunsoft.com/openapi';
}


//测试推荐环境
const RECOM_TST_URL = "http://oauth2tst.aoyunsoft.com/share";
//正式推荐环境
const RECOM_REL_URL = "http://oauth2.aoyunsoft.com/share";

Vue.http.interceptors.push(function (request, next) {
    iView.LoadingBar.start();
    loding.remove();
    loding.loadin();
    if (constant.APIENV === 'prod' || constant.APIENV === 'tst') {
        request.params.access_token = JSON.parse(localStorage.ROLE).token
    } else {
        request.params._debug = '1'
    }
    next(function (response) {
        // if (response.status == 401) {
        //   //如何token过期，则让父窗口跳到登录界面
        //    window.parent.location.hash = '#/login'
        //
        // } else if (response.status >= 400) {
        //    alert(response.data.message.info)
        // }
        iView.LoadingBar.finish();
        loding.remove();
    });
});
const API = {



    team: {

        //团队定义
        teamDefinePageQuery: API_ROOT + '/channel/team/pageQuery',

        //获取团队详细信息
        teamDetailInfo: API_ROOT + '/channel/team/getByKey',

        //更新团队信息
        updateTeamInfo: API_ROOT + '/channel/team/update',

        //保存业务员级别
        saveSaleLevel: API_ROOT + '/channel/agentLevel/save',

        //保存团队
        saveTeamInfo: API_ROOT + '/channel/team/save',

        //业务员等级字典
        salemanLevelDir: API_ROOT + '/channel/agentLevel/query',

        //业务员等级定义
        salemanLevelPageQuery: API_ROOT + '/channel/agentLevel/pageQuery',

        //获取所有团队
        allTeamInfo: API_ROOT + '/channel/team/query',

        //获取所有业务员级别
        allSalemanInfo: API_ROOT + '/channel/agentLevel/query',

        //获取业务员管理
        saleManagePageQuery: API_ROOT + '/channel/agent/pageQuery',

        //获取业务员信息
        salerInfo: API_ROOT + '/channel/agent/getByKey',

        //获取地址信息
        addressInfo: API_ROOT + '/sysmanage/address/query',

        //获取业务员级别详细信息
        saleLevelDetailInfo: API_ROOT + '/channel/agentLevel/getByKey',

        //更新业务员级别详细信息
        updatesaleLevelInfo: API_ROOT + '/channel/agentLevel/update',

        //删除业务员级别
        deleteSaleLevel: API_ROOT + '/channel/agentLevel/deleteByKey',

        //保存业务员
        saveSaleman: API_ROOT + '/channel/agent/save',

        //更新业务员信息
        updateSalerInfo: API_ROOT + '/channel/agent/update',

        //删除团队
        deleteTeam: API_ROOT + '/channel/team/deleteByKey',

        //获取业务员
        getAgentList: API_ROOT + '/channel/agent/query',

        //获取营业部
        getPartnerComList: API_ROOT + '/channel/partner/query',

        //删除业务员
        deleteSaleman: API_ROOT + '/channel/agent/deleteByKey',

    },
    recom: {

        //获取微信入口推广链接
        getRecomUrl: API_ROOT + '/dec-common/recomUrlInfo/query',

        //获取产品推广链接
        getProductRecomUrl: API_ROOT + '/product/productRecom/getProductDetail',

    }

}


function getTeamDefinePageQuery(pageSize, pageNumber, queryObj) {
    console.log("请求");
    return Vue.http.post(API.team.teamDefinePageQuery, JSON.stringify({
        pageSize: pageSize,
        pageNumber: pageNumber,
        queryObj: queryObj
    }));
}

function getTeamDetailInfo(id) {
    console.log("接口", id);
    return Vue.http.post(API.team.teamDetailInfo, JSON.stringify({
        id: id,
    }));
}

function updateTeamDetailInfo(teamInfo) {
    return Vue.http.post(API.team.updateTeamInfo, JSON.stringify(
        teamInfo
    ))
}

function saveSaleLevel(saleLevelInfo) {
    return Vue.http.post(API.team.saveSaleLevel, JSON.stringify(
        saleLevelInfo
    ))
}

function saveTeamInfo(teamName, state, partnerId) {
    return Vue.http.post(API.team.saveTeamInfo, JSON.stringify({
        teamName: teamName,
        state: state,
        partnerId: partnerId,
    }))
}

function getSalemanLevelDir(partnerId, state) {
    console.log("营业部Id", partnerId);
    console.log("状态", state);
    return Vue.http.post(API.team.salemanLevelDir, JSON.stringify({
        partnerId: partnerId,
        state: state,
    }))
}

function getSalemanLevelPageQuery(pageSize, pageNumber, queryObj) {
    return Vue.http.post(API.team.salemanLevelPageQuery, JSON.stringify({
        pageSize: pageSize,
        pageNumber: pageNumber,
        queryObj: queryObj
    }))

}

function getAllSalemanLevel(partnerId, state) {
    return Vue.http.post(API.team.allSalemanInfo, JSON.stringify({
        partnerId: partnerId,
        state: state,
    }))
}

function getAllTeamInfo(state) {
    return Vue.http.post(API.team.allTeamInfo, JSON.stringify(state))
}

//获取业务员管理列表
function getSalesManagePageQuery(pageNumber, pageSize, queryObj, total) {
    return Vue.http.post(API.team.saleManagePageQuery, JSON.stringify({
        pageNumber: pageNumber,
        pageSize: pageSize,
        queryObj: queryObj,
        total: total,
    }));
}

//通过id获取业务员信息
function getSalerInfo(id) {
    return Vue.http.post(API.team.salerInfo, JSON.stringify({
        id: id,
    }));
}

//获取地址信息
function getAddressInfo(code) {
    return Vue.http.post(API.team.addressInfo, JSON.stringify({
        parentCode: code
    }))
}

function getSaleLevelDetailInfo(id, levelName) {
    console.log("id:" + id + ";name:" + levelName);
    return Vue.http.post(API.team.saleLevelDetailInfo, JSON.stringify({
        id: id,
        levelName: levelName,
    }))
}

function updateSaleLevelInfo(saleLevelInfo) {
    return Vue.http.post(API.team.updatesaleLevelInfo, JSON.stringify(
        saleLevelInfo
    ))
}

function deleteSaleLevel(id) {
    return Vue.http.post(API.team.deleteSaleLevel, JSON.stringify({
        id: id,
    }))
}

function saveSalemanInfo(salemanInfo) {

    return Vue.http.post(API.team.saveSaleman, JSON.stringify(
        salemanInfo
    ))

}

function updateSalerInfo(updateInfo) {
    return Vue.http.post(API.team.updateSalerInfo, JSON.stringify(
        updateInfo
    ))
}

//登录
function login(userName, password) {
    Vue.http.post('http://bmptst.aoyunsoft.com/openapi/sysmanage/partner/login', JSON.stringify({
        userName: userName,
        password: password
    })).then(function (res) {
        window.localStorage.Manage = JSON.stringify(res.body.data);
    })
}

function deleteTeam(id) {
    return Vue.http.post(API.team.deleteTeam, JSON.stringify({
        id: id,
    }))
}

function getAgentList(partnerId, teamId) {
    return Vue.http.post(API.team.getAgentList, JSON.stringify({
        partnerId: partnerId,
        teamId: teamId,
    }));
}

function getPartnerComList() {
    return Vue.http.post(API.team.getPartnerComList, JSON.stringify({
        "level": "02"
    }));
}

function deleteSaleman(id) {
    return Vue.http.post(API.team.deleteSaleman, JSON.stringify({
        id: id,
    }))
}

function getRecomUrl(agentId, urlName) {
    return Vue.http.post(API.recom.getRecomUrl, JSON.stringify({
        agentId: agentId,
        urlName: urlName,
    }))
}

function getProductRecomUrl(queryObj) {
    return Vue.http.post(API.recom.getProductRecomUrl, JSON.stringify({
        pageNumber: 1,
        pageSize: 999,
        queryObj: queryObj
    }))
}

function getRecomLink() {
    return API_ENV ? RECOM_REL_URL : RECOM_TST_URL;
}

//方法裸露
export default  {
    getTeamDefinePageQuery,
    getTeamDetailInfo,
    updateTeamDetailInfo,
    saveSaleLevel,
    saveTeamInfo,
    getSalemanLevelDir,
    getSalemanLevelPageQuery,
    getAllSalemanLevel,
    getAllTeamInfo,
    getSalesManagePageQuery,
    getSalerInfo,
    getAddressInfo,
    getSaleLevelDetailInfo,
    updateSaleLevelInfo,
    deleteSaleLevel,
    saveSalemanInfo,
    updateSalerInfo,
    login,
    deleteTeam,
    getAgentList,
    getPartnerComList,
    deleteSaleman,
    getRecomUrl,
    getProductRecomUrl,
    getRecomLink,
}
